package com.future.mayi.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.future.mayi.R;
import com.future.mayi.model.SpecAttrModel;
import com.future.mayi.model.SpecItemsModel;
import com.future.mayi.model.SpecListModel;
import com.future.mayi.model.interfaces.GoodsSelectListener;
import com.future.mayi.widget.FlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GoodsSelectItemView extends LinearLayout implements View.OnClickListener {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.fl_type)
    FlowLayout flType;

    private LayoutInflater mInflater;
    private Context mContext;
    private SpecAttrModel model;
    private List<SpecListModel> spec_list;
    private List<TextView> viewList;

    private GoodsSelectListener listener;

    private int[] selectId;

    public GoodsSelectItemView(Context context) {
        super(context);
        init(context);
    }

    public GoodsSelectItemView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public GoodsSelectItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        View.inflate(context, R.layout.fragment_goods_select_item, this);
        ButterKnife.bind(this);
        mInflater = LayoutInflater.from(mContext);
    }

    public void setModel(SpecAttrModel model, List<SpecListModel> spec_list, int[] selectId) {
        if (this.model == model) return;
        this.model = model;
        this.spec_list = spec_list;
        viewList = new ArrayList<>();
        tvTitle.setText(model.getGroup_name());
        for (int i = 0; i < model.getSpec_items().size(); i++) {
            TextView tv = (TextView) mInflater.inflate(R.layout.layout_goods_label_item_text, flType, false);
            tv.setText(model.getSpec_items().get(i).getSpec_value());
            tv.setTag(model.getSpec_items().get(i));
            tv.setOnClickListener(this);
            flType.addView(tv);
            viewList.add(tv);
        }
        setSelect(selectId);
    }

    public void setListener(GoodsSelectListener listener) {
        this.listener = listener;
    }

    public SpecAttrModel getModel() {
        return model;
    }

    @Override
    public void onClick(View v) {
        if (listener == null) return;
        if (v == null) return;
        if (!(v instanceof TextView)) return;
        if (v.getTag() == null) return;
        if (!(v.getTag() instanceof SpecItemsModel)) return;
        SpecItemsModel specItemsModel = (SpecItemsModel) v.getTag();
        int position = (int) getTag();
        selectId[position] = specItemsModel.getItem_id();
        setSelect(selectId);
        listener.itemSelect(this, model, specItemsModel);
    }

    public void setSelect(int[] s) {
        if (selectId != s)
            this.selectId = s.clone();
        int position = (int) getTag();
        int thisSelectId = selectId[position];
        for (int i = 0; i < viewList.size(); i++) {
            TextView textView = viewList.get(i);
            SpecItemsModel specItemsModel = (SpecItemsModel) textView.getTag();
            int item_id = specItemsModel.getItem_id();
            selectId[position] = item_id;
            int stock_num = 0;
            for (SpecListModel specListModel : spec_list) {
                int[] skuId = specListModel.getSkuId();
                boolean exist = true;
                for (int j = 0; j < selectId.length; j++) {
                    if (!(selectId[j] == 0 || selectId[j] == skuId[j])) {
                        exist = false;
                    }
                }
                if (exist) {
                    stock_num = specListModel.getForm().getStock_num();
                    break;
                }
            }

            if (stock_num > 0) {
                if (thisSelectId == item_id) {
                    textView.setBackgroundResource(R.drawable.bg_goods_label_item_bg_t);
                    textView.setTextColor(getResources().getColor(R.color.main_color));
                    textView.setEnabled(false);
                } else {
                    textView.setBackgroundResource(R.drawable.bg_goods_label_item_bg);
                    textView.setTextColor(getResources().getColor(R.color.text_color3));
                    textView.setEnabled(true);
                }
            } else {
                textView.setEnabled(false);
                textView.setBackgroundResource(R.drawable.bg_goods_label_item_bg);
                textView.setTextColor(getResources().getColor(R.color.text_color9));
            }
        }
        selectId[position] = thisSelectId;
    }
}
